/******************************************************************************/
package jamie.org.sil.www.usxUtils;



/******************************************************************************/
/**
   Numerical processor to handle the Chinese number system.  Note that this
   only goes up as far as 9999.
   <p>

   A summary of how this works, courtesy of Mei Han Liu :-
   <p>

   To make things easy, imagine that the Chinese use the same glyphs as we
   do for the numerals 0-9, and imagine that they use h as the glyph for 100
   and t as the glyph for ten.
   <p>

   0-9 would be written in the same way as we write them.
   <p>
   &nbsp;&nbsp;&nbsp;10: 1t<br>
   &nbsp;&nbsp;&nbsp;11: 1t1<br>
   &nbsp;&nbsp;&nbsp;12: 1t2<br>
   &nbsp;&nbsp;&nbsp;...<br>
   &nbsp;&nbsp;&nbsp;20: 2t<br>
   &nbsp;&nbsp;&nbsp;...<br>
   &nbsp;&nbsp;&nbsp;200: 2h<br>
   &nbsp;&nbsp;&nbsp; 201: 2h01<br>
   &nbsp;&nbsp;&nbsp;210: 2h1t<br>
   &nbsp;&nbsp;&nbsp;211: 2h1t1<br>
   <p><br>
   
   Trying to rationalise this (and ignoring 1-9) ...
   <p>

   <ul>
   <li>The scheme is akin to the European one in that the most significant
       digit appears at the left, and that to a first approximation the
       position of a digit determines its significance.</li>

   <li>However, in addition to the place conveying significance, each digit
       has to the right of it a multiplier character which overtly makes its
       significance apparent -- t, h, etc.</li>

   <li>The exceptions to this are a) that there is no multiplier for the
       units digit; and b) that if a digit is zero, no multiplier appears.</li>

   <li>Finally, if the least significant digits of the number are all
       zeroes, they are omitted -- ie the number is truncated after the
       least significant non-zero digit.</li>
   </ul>
   
   For more information, see http://www.mandarintools.com/numbers.html.
*/

public class NumeralProcessor_Chinese extends NumeralProcessor
{
  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                               Public                                   **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  /**
   * {@inheritDoc}
   */
    
  @Override public void setInstance ()
  {
    m_Instance = this;
  }


  /****************************************************************************/
  /**
    {@inheritDoc}
  */

  @Override public int fromVernacular (String n) throws ConverterException
  {
    throw new ConverterException(ConverterException.Action.AbandonRunNow, ConverterException.Log.Error, "No support for reading Chinese numerals");
  }


  /****************************************************************************/
  /**
    {@inheritDoc}
  */

  @Override public String toVernacular (int n)
  {
    if (0 == n) return m_IntegerToGlyph.get(0);

    boolean hadDigit = false;
    int multiplier = 1;

    String res = "";
    while (n > 0)
    {
      if (multiplier > 1000) throw new ConverterException(ConverterException.Action.AbandonFileWhenever, ConverterException.Log.Error, "Can't cope with Chinese numbers greater than 9999");
      int d = n % 10;
      hadDigit = hadDigit || (0 != d);

      if (hadDigit && 0 == d)
        res = m_IntegerToGlyph.get(d) + res;
      else if (0 != d)
        res = m_IntegerToGlyph.get(d) + (multiplier > 1 ? m_IntegerToGlyph.get(multiplier) : "") + res;

      n /= 10;
      multiplier *= 10;
    }

    return res;
  }
}
